<template>
	<view class="ys">
		<view class="showLogin" v-if="show_login" @click="closeLogin()">
			<view class="bg" @click.stop>
				<view class="title">
					<image src="/static/index/logo.png" mode=""></image>
					艾泽驿站
				</view>
				<view class="h1">
					登录后体验更多服务
				</view>
				<view class="ra">
					<label class="radio" @click="changeRadio">
						<radio :checked="is_agree" />
						<view class="radiotext">已阅读并同意<text
								@click.stop="toPage('/pages/about/index?type=customer_agree')">《用户隐私协议》</text></view>
					</label>
				</view>
				<view class="btns">
					<!-- #ifdef H5 -->
					<button @click="toLogin" class="btn">立即登录</button>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<button v-if="hasUser && is_agree" class="btn" @click="userLogin">立即登录</button>
					<button v-else-if="is_agree && !hasUser" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
						class="btn">立即登录</button>
					<button v-else @click="showTips" class="btn">立即登录</button>
					<!-- #endif -->
					<text class="out" @click="closeLogin">暂不登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUserInfo,
		GetOpenid,
		GetPhone
	} from "@/api/api.js"
	export default {
		data() {
			return {
				is_agree: false,
				hasUser: false
			}
		},
		props: ['show_login', 'refer_id'],
		mounted() {
			this.getMpOpenid()
		},
		methods: {
			closeLogin() {
				this.$emit('closeLogin', false)
			},
			showTips() {
				uni.$showMsg('请阅读并同意相关协议')
			},
			toLogin() {
				if (!this.is_agree) {
					uni.$showMsg('请阅读并同意相关协议')
				} else {
					uni.navigateTo({
						url: '/pages/login/index'
					})
				}
			},
			changeRadio(e) {
				this.is_agree = !this.is_agree
			},
			getMpOpenid() {
				var that = this
				// #ifdef MP-WEIXIN
				uni.login({
					success(res) {
						GetOpenid({
							code: res.code
						}).then(data => {
							that.open_id = data.data.openid
							that.hasUser = data.data.hasUser
						})
					}
				})
				// #endif
			},
			userLogin() {
				GetPhone({
					openid: this.open_id,
					refer_id: this.refer_id, //邀请人
				}).then(res => {
					if (res.code == 0) {
						let data = res.data
						uni.setStorageSync('token', data.token)
						this.$emit('getLogin', false)
						this.$emit('closeLogin', false)
						this.$emit('getUser', true)
					}
				})
			},
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					let encryptedData = e.detail.encryptedData
					let errMsg = e.detail.errMsg
					let iv = e.detail.iv
					let code = e.detail.code
					GetPhone({
						openid: this.open_id,
						refer_id: this.refer_id, //邀请人
						code
					}).then(res => {
						if (res.code == 0) {
							let data = res.data
							uni.setStorageSync('token', data.token)
							this.$emit('getLogin', false)
							this.$emit('closeLogin', false)
							this.$emit('getUser', true)
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.showLogin {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		background: #000000c0;
		z-index: 999;

		.bg {
			background: #fff;
			border-top-left-radius: 18px;
			border-top-right-radius: 18px;
			padding: 15px;
			box-sizing: border-box;
			width: 100%;
			position: absolute;
			bottom: 0;

			.title {
				display: flex;
				align-items: center;
				font-weight: bold;
				font-size: 17px;
				margin-top: 10px;

				image {
					width: 25px;
					height: 25px;
					margin-right: 5px;
					border-radius: 50%;
				}
			}

			.h1 {
				font-size: 18px;
				font-weight: bold;
				margin-top: 20px;
			}

			.ra {
				margin-top: 15px;

				label {
					display: flex;
					align-items: center;

					.radiotext {
						text {
							color: #FF3C29;
						}
					}
				}
			}

			.btns {
				display: flex;
				align-items: center;
				flex-direction: column;
				justify-content: center;
				margin-top: 30px;
				color: #777;

				.btn {
					width: 96%;
					font-size: 14px;
					border-radius: 20px;
				}
			}
		}
	}
</style>